from rest_framework.views import APIView
from utils.payment import AliPayUtil, OrderUtil
from utils.result import R
from django.http import HttpResponse
from utils.status_code import StatusCodeEnum
from api.models import ItEduVipOrder,ItEduLessonVideoSet,ItEduLessonOrder

class VipPayAPIView(APIView):

    def put(self,requset):
        """创建订单

        Args:
            requset (_type_): _description_
        """
        user_pk = requset.session.get("user_pk")
        
        time_type = requset.data.get("time_type","month")
        vip_type = requset.data.get("vip_type","pro")
        
        price = 0 # VIP的价格
        
        if time_type == "month":
            time_type = "月付VIP"
            if vip_type == "plus":
                price = 5 
            else:
                price = 10 
        else:
            time_type = "年付VIP"
            if vip_type == "plus":
                price = 20 
            else:
                price = 30 
                
        vip_name = "{},{}".format(time_type,vip_type)
        order_id = OrderUtil.order_id()
        callback_url = OrderUtil.get_web_callback_url("vip")
        pay_url = AliPayUtil.ali_web_pay(vip_name,str(price),order_id,callback_url,"")

        # 创建订单
        ItEduVipOrder.objects.create(
             user___id = user_pk,
             vip = vip_name,
             price = price,
             orderId = order_id,
        )
        
        return R().data_result(pay_url = pay_url,order_id=order_id)
        
    def post(self,request):
        """支付成功的回调
        
        !!!!!!!!!!恶意回调（注意）

        Args:
            request (_type_): _description_
        """
        order_id = request.data.get("out_trade_no")

        vip_order = ItEduVipOrder.objects.get(orderId=order_id)
        
        if vip_order.status == "已支付":
            return HttpResponse("success")
        
        buy_user = vip_order.user 
        
        buy_user.vip_type = vip_order.vip
        buy_user.user_info.credit += vip_order.price
        
        buy_user.save()
        
        vip_order.status = "已支付" 

        return HttpResponse("success")
    
    def delete(self,request):
        """查看订单是否支付

        Args:
            request (_type_): _description_
        """
        order_id = request.data.get("order_id")
        
        vip_order = ItEduVipOrder.objects.get(orderId=order_id)

        if vip_order.status == "已支付":
            return R.ok().to_json_response()
        
        return R.other_result(StatusCodeEnum.WAIT_PAY).to_json_response()
    
"""
课程支付接口
"""
class LessonPayAPIView(APIView):

    def put(self,requset):
        """创建订单

        Args:
            requset (_type_): _description_
        """
        user_pk = requset.session.get("user_pk")

        lesson_set_id = requset.data.get("lesson_set_id")

        lesson_set = ItEduLessonVideoSet.objects.get(pk=lesson_set_id)
        price = lesson_set.price
        lesson_name = lesson_set.title
                
        order_id = OrderUtil.order_id()
        callback_url = OrderUtil.get_web_callback_url("lesson")
        pay_url = AliPayUtil.ali_web_pay(lesson_name,str(price),order_id,callback_url,"")

        # 创建订单
        ItEduLessonOrder.objects.create(
             user___id = user_pk,
             orderId = order_id,
             lesson___id = lesson_set_id
        )
        
        return R().data_result(pay_url = pay_url,order_id=order_id)
        
    def post(self,request):
        """支付成功的回调
        
        !!!!!!!!!!恶意回调（注意）

        Args:
            request (_type_): _description_
        """
        order_id = request.data.get("out_trade_no")

        lesson_order = ItEduLessonOrder.objects.get(orderId=order_id)
        
        if lesson_order.status == "已支付":
            return HttpResponse("success")
        
        buy_user = lesson_order.user 
        lesson = lesson_order.lesson
        
        buy_user.user_info.credit += lesson.price
        
        buy_user.save()
        
        lesson_order.status = "已支付" 

        return HttpResponse("success")
    
    def delete(self,request):
        """查看订单是否支付

        Args:
            request (_type_): _description_
        """
        order_id = request.data.get("order_id")
        
        lesson_order = ItEduLessonOrder.objects.get(orderId=order_id)

        if lesson_order.status == "已支付":
            return R.ok().to_json_response()
        
        return R.other_result(StatusCodeEnum.WAIT_PAY).to_json_response()
   